package com.flyweb.result.type;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

/**
 * <p>
 * Title: flyweb
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * CreateTime: 2007-Oct 16, 2007-7:33:18 AM
 * </p>
 * 
 * @author fireflyc@126.com
 */
public class Freemarker extends BaseResult {

	public void execute(HttpServletRequest request,
			HttpServletResponse response, ServletContext context) {
		Configuration cfg = new Configuration();
		cfg.setServletContextForTemplateLoading(context, this.getTypeValue());
		// - Set update dealy to 0 for now, to ease debugging and testing.
		// Higher value should be used in production environment.
		cfg.setTemplateUpdateDelay(0);
		// - Set an error handler that prints errors so they are readable with
		// a HTML browser.
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
		// - Use beans wrapper (recommmended for most applications)
		cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);

		try {
			Template t = cfg.getTemplate(this.getValue());
			t.process(this.getRoot(), response.getWriter());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TemplateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
